/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systemtest;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.GregorianCalendar;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import se392.ateam2006.meetingmgmt.MeetingMgmtRemote;
import se392.ateam2006.meetingmgmt.meeting.Duration;
import se392.ateam2006.meetingmgmt.meeting.MeetingEntity;
import se392.ateam2006.resourcemgmt.ResourceMgmtRemote;
import se392.ateam2006.resourcemgmt.room.RoomEntity;
import se392.ateam2006.user.UserEntity;
import se392.ateam2006.user.UserMgmtRemote;

/**
 *
 * @author Caroline
 */
public class Main {

    //private ManagementRemoteHome home;
    private MeetingMgmtRemote meetingManage;
    private ResourceMgmtRemote resourceManage;
    private UserMgmtRemote userManage;

    public void setup() {
        System.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
        System.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

        try {
            InitialContext jndicontext = new InitialContext();
            meetingManage = (MeetingMgmtRemote) jndicontext.lookup("se392.ateam2006.meetingmgmt.MeetingMgmtRemote");

            resourceManage = (ResourceMgmtRemote) jndicontext.lookup("se392.ateam2006.resourcemgmt.ResourceMgmtRemote");

            userManage = (UserMgmtRemote) jndicontext.lookup("se392.ateam2006.user.UserMgmtRemote");

        } catch (NamingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        resourceManage.setTestingMode(true);

    }

    public void createMeeting(String initiator, String id, String startDateRange,
            String endDateRange, String duration, String title, String description, String venue, int size) throws Exception {
        UserEntity initiator_obj = userManage.createUser(initiator);

        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd kk:mm");
        GregorianCalendar startDateRangeCal = new GregorianCalendar();
        GregorianCalendar endDateRangeCal = new GregorianCalendar();
        startDateRangeCal.setTime(format.parse(startDateRange));
        endDateRangeCal.setTime(format.parse(endDateRange));

        if (startDateRangeCal.HOUR == 24) {
            System.err.println("PROBLEM WITH DATES: Text: " + startDateRange + ", date: " + startDateRangeCal.getTime().toString());
        }

        int hours;
        int minutes;
        int days;
        String[] parts = duration.split(" ");
        days = Integer.parseInt(parts[0]);
        parts = parts[1].split(":");
        hours = Integer.parseInt(parts[0]);
        minutes = Integer.parseInt(parts[1]);

        Duration duration_obj = meetingManage.createDuration(days, hours, minutes);
        int roomSize = 30;
        String equipment = "Projector";

        RoomEntity room = null;
        if (!venue.equals("blank")) {
            if ((room = resourceManage.findRoomByID(venue)) == null) {
                throw new Exception();
            }
        }

        if (description.equals("blank")) {
            description = "";
        }
        meetingManage.createMeeting(initiator_obj, id, title, description, startDateRangeCal, endDateRangeCal, duration_obj, size, equipment, room);
    }

    public void getAllMeetings() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        SimpleDateFormat durationFormat = new SimpleDateFormat("s H:mm");
        ArrayList<MeetingShell> meetingList = new ArrayList();
        Collection<MeetingEntity> tempMees = meetingManage.getAllMeetings();
        String theVenue;

        for (MeetingEntity mee : tempMees) {
            String startDate = mee.getStartDateRange().YEAR + "/" + mee.getStartDateRange().MONTH + "/" + mee.getStartDateRange().DAY_OF_MONTH + " " + mee.getStartDateRange().HOUR + ":" + mee.getStartDateRange().MINUTE;
            System.out.println(startDate);
            meetingList.add(new MeetingShell(
                    mee.getInitiator().getEmail(),
                    mee.getId(),
                    format.format(mee.getStartDateRange().getTime()),
                    format.format(mee.getEndDateRange().getTime()),
                    mee.getDuration().toString(),
                    mee.getTitle(),
                    mee.getDescription(),
                    this.extractVenue(mee.getRoom()),
                    mee.getRequestedRoomSize()));

            System.out.println(format.format(mee.getStartDateRange().getTime()));
            System.out.println("start date range " + mee.getStartDateRange().HOUR);
        }

        System.out.println(meetingList.get(0).startDateRange);
        System.out.println("bob");
    }

    public void bookMeetingAtDateTimeVenue(String meetingId, String theStartDate, String venue) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd kk:mm");
        GregorianCalendar startDateCal = new GregorianCalendar();
        try {
            startDateCal.setTime(format.parse(theStartDate));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        meetingManage.bookMeeting(meetingId, venue, startDateCal);


    }

    public void TearDown() {
        meetingManage.removeAllMeetings();
        resourceManage.removeAllExclusionSets();
        userManage.removeAllUsers();
        resourceManage.removeAllRooms();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        Main main = new Main();
        main.setup();
       //   main.createMeeting("sac4@aber.ac.uk", "sac4_1", "2006/01/04 10:00", "2006/01/05 11:00", "0 1:00", "Simple", "blank", "blank", -1);
        main.bookMeetingAtDateTimeVenue("sac4_1", "2005/01/05 10:10", "502");
        //main.getAllMeetings();
        main.TearDown();
    }

    private String extractVenue(RoomEntity room) {
        if (room == null || room.getRoomID() == null) {
            return "";
        } else {
            return room.getRoomID();
        }
    }
}
